import path from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// import proxy from './config/proxy';
import * as env from './config/env';

const BASE_PATH = '/wowon';
const PORT = 8299;

// ENV: 开发环境，运行默认进测试环境，打包默认进生产环境，打包测试环境执行：pnpm build:test
// ENV_PROXY: 本地代理，默认测试环境，可以手动进入生产环境：pnpm dev --pxy=prod
const { ENV, ENV_PROXY } = process.argv.reduce(
  (res, arg) => {
    let name = '';
    if (/^\-\-env\=/.test(arg)) name = 'ENV';
    if (/^\-\-pxy\=/.test(arg)) name = 'ENV_PROXY';
    name && (res[name] = arg.split('=')[1]);
    return res;
  },
  { ENV: 'prod', ENV_PROXY: 'test' },
);

console.log(`⚡ 运行环境：${ENV} | 代理环境：${ENV_PROXY} 执行中... ⚡`);

const envConfig = env[ENV];

if (!envConfig) console.log('！！！没有环境配置：', ENV);

// https://vite.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': path.join(__dirname, './src'),
      '@wowon/components': path.join(__dirname, './src/components/@comp'),
      '@wowon/layouts': path.join(__dirname, './src/components/@layout'),
      '@wowon/hooks': path.join(__dirname, './src/components/@hooks'),
      // UI组件库，实现切换组件库，做了不同组件库间的属性兼容
      ui: path.join(__dirname, './src/components/@lib/element-plus'),
      'ui-icons': path.join(__dirname, './src/components/@lib/element-plus-icons'),
      // 'ui': path.join(__dirname, './src/components/@lib/antd-vue'),
      // 'ui-icons': path.join(__dirname, './src/components/@lib/antd-vue-icons'),
    },
  },
  define: {
    ENV: `'${ENV}'`,
    BASE_PATH: `'${BASE_PATH}'`,
    APP_VERSION: `'${process.env.npm_package_version}'`,
    APP_BUILD_TIME: `'${new Date().toLocaleString()}'`,
    MAP_AK: `'${env.MAP_AK}'`,
    SITE_TITLE: `'${envConfig.SITE_TITLE}'`,
    SITE_URL: `'${envConfig.SITE_URL}'`,
    PATH_PUBLIC: `'${envConfig.PATH_PUBLIC}'`,
    PATH_UPLOAD: `'${envConfig.PATH_UPLOAD}'`,
    API_SERVER_BASE: `'${envConfig.API_SERVER_BASE}'`,
  },
  base: BASE_PATH + '/', // 根路径
  publicDir: BASE_PATH + '/', // 静态文件路径
  // build: { outDir: 'dist' },
  server: { port: PORT },
  plugins: [vue()],
});
